package com.yb.xczx.cloud.auth.handler;

import com.yb.core.enums.HttpCode;
import com.yb.core.model.XczxResult;
import com.yb.xczx.cloud.auth.util.CacheUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/21 12:42
 * FileName: XczxAuthenticationEntryPoint
 * Description:
 */
public class XczxOauth2AuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();

        String targetUrl=requestURI+"?"+queryString;

        String targetUrlKey = UUID.randomUUID().toString();
        CacheUtil.targetUrlMap.put(targetUrlKey,targetUrl);
        String loginUrl="/login.html?targetUrlKey="+targetUrlKey;
        response.sendRedirect(loginUrl);
    }
}
